"""
Case Type   : 基础功能
Case Name   : 修改数据库，用户，会话级别的参数idle_in_transaction_session_timeout
Create At   : 2023/01/03
Owner       : haomeng802
Description :
    1.查看idle_in_transaction_session_timeout默认值
    2.修改数据库，用户，会话级别的参数idle_in_transaction_session_timeout
    3.恢复默认值
Expect      :
    1.成功
    2.设置成功
    3.恢复成功
History     :
"""

import os
import unittest

from yat.test import Node

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class IdleSession(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        text = f'----{os.path.basename(__file__)} start----'
        self.log.info(text)
        self.sh_primysh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.node = Node(node='PrimaryDbUser')
        self.database_name = self.node.db_name
        self.node_user = self.node.db_user

    def test_idlesession(self):
        step_text1 = "----step1：查看idle_in_transaction_session_timeout默认值；" \
                     "expect：成功----"
        self.log.info(step_text1)
        self.show_sql = 'show idle_in_transaction_session_timeout;'
        self.log.info(self.show_sql)
        result_show = self.sh_primysh.execut_db_sql(self.show_sql)
        self.log.info(result_show)
        self.assertIn('0', result_show, '执行失败' + step_text1)

        step_text2 = "----step2：在gsql中设置数据库、用户、会话级别" \
                     "idle_in_transaction_session_timeout;expect：设置成功----"
        self.log.info(step_text2)
        alter_sql = f"alter database {self.database_name} set " \
                    f"idle_in_transaction_session_timeout to 30;" \
                    f"alter user {self.node_user} set " \
                    f"idle_in_transaction_session_timeout to 30;" \
                    f"set idle_in_transaction_session_timeout to 30;" \
                    f"show idle_in_transaction_session_timeout;"
        result_sql = self.sh_primysh.execut_db_sql(alter_sql)
        self.log.info(result_sql)
        self.assertIn(self.constant.ALTER_DATABASE_SUCCESS_MSG, result_sql,
                      "执行失败" + step_text2)
        self.assertIn(self.constant.ALTER_ROLE_SUCCESS_MSG, result_sql,
                      "执行失败" + step_text2)
        self.assertIn(self.constant.SET_SUCCESS_MSG, result_sql,
                      "执行失败" + step_text2)
        self.assertIn('30', result_sql, '执行失败' + step_text1)

    def tearDown(self):
        step_text3 = "----step3：恢复默认值；expect：成功----"
        self.log.info(step_text3)
        alter_database_sql = f"alter database {self.database_name} " \
                             f"set idle_in_transaction_session_timeout to 0;"
        result_database = self.sh_primysh.execut_db_sql(alter_database_sql)
        self.log.info(result_database)
        alter_user_sql = f"alter user {self.node_user} set " \
                         f"idle_in_transaction_session_timeout to 0;"
        result_user = self.sh_primysh.execut_db_sql(alter_user_sql)
        self.log.info(result_user)
        self.assertIn(self.constant.ALTER_DATABASE_SUCCESS_MSG, result_database,
                      "执行失败" + step_text3)
        self.assertIn(self.constant.ALTER_ROLE_SUCCESS_MSG, result_user,
                      "执行失败" + step_text3)
        self.log.info(f'----{os.path.basename(__file__)} end----')